package com.qlh.camera

import com.sun.jna.Native
import com.sun.jna.NativeLong
import com.sun.jna.Pointer
import com.sun.jna.examples.win32.W32API
import com.sun.jna.ptr.ByteByReference
import com.sun.jna.ptr.NativeLongByReference
import java.awt.Panel

/******************************************************************************
 *  FRealDataCallBack
 * 实现预览回调数据
 */
class FRealDataCallBack constructor(val panelRealplay: Panel, val playControl: PlayCtrl, val m_lPort: NativeLongByReference) : HCNetSDK.FRealDataCallBack_V30 {

    //预览回调
    override fun invoke(lRealHandle: NativeLong?, dwDataType: Int, pBuffer: ByteByReference?, dwBufSize: Int, pUser: Pointer?) {
        val hwnd = W32API.HWND(Native.getComponentPointer(panelRealplay))
        println(dwDataType.toInt())
        when (dwDataType) {
            HCNetSDK.NET_DVR_SYSHEAD -> {//系统头
                if (!CameraClient.playControl.PlayM4_GetPort(m_lPort)) //获取播放库未使用的通道号
                    return

                if (dwBufSize > 0) {
                    if (!CameraClient.playControl.PlayM4_SetStreamOpenMode(m_lPort.getValue(), PlayCtrl.STREAME_REALTIME))//设置实时流播放模式
                        return

                    if (!CameraClient.playControl.PlayM4_OpenStream(m_lPort.getValue(), pBuffer, dwBufSize, 1024 * 1024)) //打开流接口
                        return
                    if (!CameraClient.playControl.PlayM4_Play(m_lPort.getValue(), hwnd)) //播放开始
                        return
                }
                if (dwBufSize > 0 && m_lPort.getValue().toInt() != -1) {
                    if (!CameraClient.playControl.PlayM4_InputData(m_lPort.getValue(), pBuffer, dwBufSize)) //输入流数据
                        return
                }
            }
            HCNetSDK.NET_DVR_STREAMDATA ->  //码流数据
                if (dwBufSize > 0 && m_lPort.getValue().toInt() != -1) {
                    if (!CameraClient.playControl.PlayM4_InputData(m_lPort.getValue(), pBuffer, dwBufSize))
                        return
                }
        }
    }
}